using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace TR0217.ControlEx
{
    internal class NavigateContextMenu : Control
    {
        private NavigateBar _navigateBar;

        private NavigateMenuItemCollection _items;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public NavigateMenuItemCollection Items
        {
            get { return _items; }
        }

        public event SelectedChangedHandler ItemSelect;

        public NavigateContextMenu(NavigateBar navigateBar)
        {
            _navigateBar = navigateBar;
            _items = new NavigateMenuItemCollection(navigateBar);
            SetStyle(
                ControlStyles.UserPaint |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.SupportsTransparentBackColor |
                ControlStyles.ResizeRedraw, true);

            this.SetStyle(ControlStyles.Selectable, false);
            this.SetStyle(ControlStyles.StandardClick, true);
            this.SetStyle(ControlStyles.StandardDoubleClick, true);
            this.UpdateStyles();
        }

        NavigateMenuItem _hotItem = null;
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            _hotItem = null;
            foreach (NavigateMenuItem item in _items)
            {
                Rectangle rect = new Rectangle(0, (int)item.Top,Width, (int)item.Height);
                if (rect.Contains(e.Location))
                    _hotItem = item;
            }
            this.Invalidate();
        }

        NavigateMenuItem _selectedItem = null;
        internal NavigateMenuItem SelectedItem
        {
            get { return _selectedItem; }
            set { _selectedItem = value; }
        }
        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            if (_hotItem != null)
            {
                Rectangle rect = new Rectangle(0, (int)_hotItem.Top, Width, (int)_hotItem.Height);
                if (rect.Contains(e.Location))
                {
                    _hotItem.Selected = true;
                    _selectedItem = _hotItem;
                    if (ItemSelect != null)
                    {
                        ItemSelect(this, new SelectedChangedEventArgs(true, _selectedItem));
                    }
                }
            }

            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            
            Rectangle rect = new Rectangle(0, 0, this.Width-1, this.Height-1);
            LinearGradientBrush gradientBrush = new LinearGradientBrush(rect, _navigateBar.BeginColor, _navigateBar.EndColor, _navigateBar.GradientMode);
            e.Graphics.FillRectangle(gradientBrush, rect);
            gradientBrush.Dispose();

            SolidBrush brushFore = new SolidBrush(_navigateBar.ForeColor);
            Pen p = new Pen(brushFore);
            e.Graphics.DrawRectangle(p, rect);
            p.Dispose();
            foreach (NavigateMenuItem item in _items)
            {
                if (!item.Visible) continue;
                if (item != _selectedItem)
                    item.Selected = false;

                if (item.Selected)
                {
                    SolidBrush brushSelectedFore = new SolidBrush(_navigateBar.SelectedForeColor);
                    if (item == _hotItem)
                    {
                        Font font = new Font(_navigateBar.Font, FontStyle.Bold);
                        e.Graphics.DrawString(item.Text, font, brushSelectedFore, item.Left, item.Top);
                        font.Dispose();
                    }
                    else
                        e.Graphics.DrawString(item.Text, _navigateBar.Font, brushSelectedFore, item.Left, item.Top);

                    brushSelectedFore.Dispose();
                }
                else
                {
                    if (item == _hotItem)
                    {
                        Font font = new Font(_navigateBar.Font, FontStyle.Bold);
                        e.Graphics.DrawString(item.Text, font, brushFore, item.Left, item.Top);
                        font.Dispose();
                    }
                    else
                        e.Graphics.DrawString(item.Text, _navigateBar.Font, brushFore, item.Left, item.Top);
                }
            }
            brushFore.Dispose();
        }
    }
}
